package com.example.controller;

import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;
import android.widget.TextView;

import com.example.listenanddriveproject.R;

public class HomographieFragment extends Fragment{
	
	private Context context;
	private View rootView;
	private SeekBar seekBarDeformation;
	private TextView textDeformation;
	private TextView textZoom;
	private SeekBar seekBarZoom;
	private Button button;
	private int progressDeformation;
	private int progressZoom;
	
	public View onCreateView (LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
		
		rootView = inflater.inflate(R.layout.fragment_homographie, container, false);
		
		context = this.getActivity();
		
		textDeformation = (TextView) rootView.findViewById(R.id.textViewDeformation);
		textDeformation.setText(String.valueOf(MainActivity.settings.getHomography().getDeformation()));
		
		seekBarDeformation = (SeekBar) rootView.findViewById(R.id.seekBarDeformation);
		seekBarDeformation.setProgress(MainActivity.settings.getHomography().getDeformation());
		seekBarDeformation.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {       

		    @Override       
		    public void onStopTrackingTouch(SeekBar seekBar) {      
		        // TODO Auto-generated method stub      
		    }       
	
		    @Override       
		    public void onStartTrackingTouch(SeekBar seekBar) {     
		        // TODO Auto-generated method stub      
		    }       
	
		    @Override       
		    public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
		    	textDeformation.setText(String.valueOf(progress));
		    	progressDeformation=progress;
		    }       
	    }); 
	    
		textZoom = (TextView) rootView.findViewById(R.id.textViewZoom);
		textZoom.setText(String.valueOf(MainActivity.settings.getHomography().getZoom()));
		
	    seekBarZoom = (SeekBar) rootView.findViewById(R.id.seekBarZoom);
	    seekBarZoom.setProgress(MainActivity.settings.getHomography().getZoom());
	    seekBarZoom.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {       

		    @Override       
		    public void onStopTrackingTouch(SeekBar seekBar) {      
		        // TODO Auto-generated method stub      
		    }       
	
		    @Override       
		    public void onStartTrackingTouch(SeekBar seekBar) {     
		        // TODO Auto-generated method stub      
		    }       
	
		    @Override       
		    public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
		    	textZoom.setText(String.valueOf(progress));
		    	progressZoom=progress;
		    }       
	    });
	    
	    button = (Button) rootView.findViewById(R.id.Apply1);
        button.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
		    	MainActivity.settings.getHomography().setZoom(progressDeformation);
		    	MainActivity.settings.getHomography().setDeformation(progressZoom);
            	MainActivity.settings.saveArray(context);
            }
        });
        
		return rootView;
	}
	
	/***
	 * refresh the UI when the reset is called
	 */
	@Override
	public void onResume(){
		super.onResume();
		
		if(MainActivity.settings.getHomography().isRefresh()){
			textDeformation.setText(String.valueOf(MainActivity.settings.getHomography().getDeformation()));
			seekBarDeformation.setProgress(MainActivity.settings.getHomography().getDeformation());
			
			textZoom.setText(String.valueOf(MainActivity.settings.getHomography().getZoom()));
			seekBarZoom.setProgress(MainActivity.settings.getHomography().getZoom());
			
			MainActivity.settings.getHomography().setRefresh(false);
		}
	}
}
